//
//  LogingViewController.m
//  PatientApp
//
//  Created by chirui on 2017/5/16.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "LogingViewController.h"
#import "ForgetCodeViewController.h"
#import "RegistUserViewController.h"
#import "TabBarVc.h"


@interface LogingViewController ()
@property (weak, nonatomic) IBOutlet UIView *PhoneView;
@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (weak, nonatomic) IBOutlet UIView *passwordView;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UIButton *logingBtn;

@end

@implementation LogingViewController

-(void)viewWillDisappear:(BOOL)animated {
    

    self.navigationController.navigationBarHidden =NO;
}

-(void)viewWillAppear:(BOOL)animated {
    
    
    self.navigationController.navigationBarHidden = YES; //隐藏
    
    NSString * phoneStr  = USER_DEFAULTGet(@"phone");
    NSString * passwordStr = USER_DEFAULTGet(@"passwd");
    
    if (phoneStr.length>0) {
        _phoneText.text  =  phoneStr;
    }
    if (passwordStr.length>0) {
        
        _passwordText.text = passwordStr;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.logingBtn.layer.cornerRadius = 25.f;
    self.logingBtn.layer.masksToBounds = YES;
    
    
    
    
}

- (IBAction)back:(id)sender {
    
//    [self.navigationController popViewControllerAnimated:YES];
    
    SHuiMBhudTextTimeStr(@"登录成功自动返回！");
    
    return;
}

- (IBAction)forgetClick:(UIButton *)sender {
    
    ForgetCodeViewController * forget =[[ForgetCodeViewController alloc]init];
    
    [self presentViewController:forget animated:YES completion:^{
        
    }];
//    [self.navigationController pushViewController:forget animated:YES];
    
}
- (IBAction)logingBtnClick:(UIButton *)sender {
    
    if ([SHJudgeTools valiMobile:self.phoneText.text] != nil) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.yOffset = 100.f;
        hud.labelText = [SHJudgeTools valiMobile:self.phoneText.text];
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:0.5];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [hud removeFromSuperview];
        });
        return;
    }else if (self.phoneText.text.length!=11) {
        
        [SHJudgeTools MBProgressHUDPromptWith:@"手机号码不正确" On:self.view];
        return;
        
    }else if(_passwordText.text.length<1){
        
        [SHJudgeTools MBProgressHUDPromptWith:@"密码不能为空" On:self.view];
        return;
        
    }else if(_passwordText.text.length>8){
        [SHJudgeTools MBProgressHUDPromptWith:@"密码不能大于8位" On:self.view];
        return;
    }else{
        
        USER_DEFAULTSet(_phoneText.text, @"phone");
        USER_DEFAULTSet(_passwordText.text, @"passwd");
    
     [self logingNetWorking];
    }
    
   
}
- (IBAction)reginBtnClick:(UIButton *)sender {
    
    RegistUserViewController * regist =[[RegistUserViewController alloc]init];
    [self presentViewController:regist animated:YES completion:^{
        
    }];
//    [self.navigationController pushViewController:regist animated:YES];
}

//登录
-(void)logingNetWorking{
    
    
    NSString * md5Password = [SHJudgeTools md532BitLower:_passwordText.text];
    NSString * md5ver = [SHJudgeTools md532BitLower:[NSString stringWithFormat:@"%@api.shangpibao.com",_phoneText.text]];
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@",address_url,login_api];
    NSDictionary * parameter = @{@"uphone":_phoneText.text,
                                 @"upasswords":md5Password,
                                 @"ver":md5ver
                                };
    NSLog(@"param ==%@",parameter);
    
    SHuiMBhubNoTimeStr(@"正在登录");
    [PostNetWorking postNetWorkingStr:parametersStr  andparameters:parameter  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"err"] intValue]==0) {
            SHuiMBhudTextTimeStr(@"登录成功");
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                

                USER_DEFAULTSet(responseObject[@"data"][0], @"userData");
                USER_DEFAULTSet(@"ok", @"enter");
                USER_DEFAULTSet(responseObject[@"data"][0][@"uid"], @"uid");
                USER_DEFAULTSet(responseObject[@"data"][0][@"ucid"], @"classId");
                 USER_DEFAULTSet(responseObject[@"data"][0][@"ujd"], @"ujd");
            
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW , (1 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{
                    
                  
                    TabBarVc * tabbar  =[[TabBarVc alloc]init];
                    [self presentViewController:tabbar animated:YES completion:^{
                        
                    }];
                });
            });

            
        } else {
            
            SHuiMBhudTextTimeStr(EorrMessageStr);
        }
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"请检查您的网络!");
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
